package me.edzh.spring.bootstrap.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.log4j.Log4j;
import lombok.var;
import me.edzh.spring.bootstrap.common.GlobalPropertiesConfigurer;
import me.edzh.spring.bootstrap.service.SMSService;
import me.edzh.spring.bootstrap.utils.ObjectMapperFactory;
import me.edzh.spring.bootstrap.utils.ResultCode;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Log4j
@Service("aliyunSMS")
public class AliyunSMSServiceImpl implements SMSService {
    private IAcsClient client;
    private String defaultSignName;
    private List<String> availableSignNames = new ArrayList<>();

    @PostConstruct
    private void init() {
        String ak = GlobalPropertiesConfigurer.getProperty("aliyun.sms.ak");
        String as = GlobalPropertiesConfigurer.getProperty("aliyun.sms.sk");
        defaultSignName = GlobalPropertiesConfigurer.getProperty("aliyun.sms.signs.default");
        var signs = GlobalPropertiesConfigurer.getProperty("aliyun.sms.signs");
        if (!TextUtils.isEmpty(signs)) {
            availableSignNames.addAll(Arrays.asList(signs.split(",")));
        }
        if (TextUtils.isEmpty(ak) || TextUtils.isEmpty(as) || TextUtils.isEmpty(defaultSignName)) {
            return;
        }
        log.info("sms inited for sign:" + defaultSignName);


        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ak, as);
        client = new DefaultAcsClient(profile);
    }
    @Override
    public void sendSms(String number, String templateId, Map<String, String> params) {
        try {
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", number);
            request.putQueryParameter("SignName", defaultSignName);
            request.putQueryParameter("TemplateCode", templateId);
            request.putQueryParameter("TemplateParam", ObjectMapperFactory.getObjectMapper().writeValueAsString(params));
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
        } catch (Throwable e) {
            log.error(e);
            throw new ServiceException(ResultCode.ERROR_SMS_CODE_DOMAIN, "短信发送失败");
        }
    }

    @Override
    public void sendSms(String number, String sign, String templateId, Map<String, String> params) {
        if (!availableSignNames.contains(sign)) {
            throw new ServiceException(ResultCode.ERROR_INTERNAL_DATA_CODE, "sign:" + sign + " invalid, please set configuration key of aliyun.sms.signs");
        }

        try {
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", number);
            request.putQueryParameter("SignName", sign);
            request.putQueryParameter("TemplateCode", templateId);
            request.putQueryParameter("TemplateParam", ObjectMapperFactory.getObjectMapper().writeValueAsString(params));
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
        } catch (Throwable e) {
            log.error(e);
            throw new ServiceException(ResultCode.ERROR_SMS_CODE_DOMAIN, "短信发送失败");
        }
    }
}
